package com.ruoyi.app.controller.project;

import com.ruoyi.app.business.project.ProjectBusiness;
import com.ruoyi.app.business.project.ProjectDeliveryBusiness;
import com.ruoyi.app.config.annotation.OperationAuth;
import com.ruoyi.app.config.annotation.ReSubmitFilter;
import com.ruoyi.app.domain.common.UploadResult;
import com.ruoyi.app.domain.project.ProjectDetail;
import com.ruoyi.app.domain.project.ProjectObj;
import com.ruoyi.app.domain.project.ProjectSimple;
import com.ruoyi.app.domain.project.change.ProjectChangeRecord;
import com.ruoyi.app.domain.project.delivery.ProjectDelivery;
import com.ruoyi.app.domain.project.upload.TeamUploadResult;
import com.ruoyi.app.domain.project.verify.ProjectVerify;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.common.core.web.domain.APIResult;
import com.ruoyi.common.core.web.page.CommonPageInfo;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.service.project.query.ProjectLogBaseQuery;
import com.ruoyi.service.project.query.ProjectQueryForDemander;
import com.ruoyi.service.project.query.ProjectQueryForLabor;
import com.ruoyi.service.project.query.ProjectQueryForPlatform;
import com.ruoyi.service.user.enums.UserTypeEnum;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目管理
 *
 * @author Zane
 * @since 2025-10-10
 */
@RestController
@RequestMapping("/project/")
public class ProjectController {

  @Resource
  private ProjectBusiness projectBusiness;
  @Resource
  private ProjectDeliveryBusiness projectDeliveryBusiness;

  /**
   * 项目方项目列表
   */
  @OperationAuth({UserTypeEnum.DEMANDER_USER})
  @PostMapping("/listForDemander")
  public TableDataInfo<ProjectSimple> listForDemander(
      @Validated @RequestBody ProjectQueryForDemander projectQuery) {
    CommonPageInfo<ProjectSimple> projects = projectBusiness.listForDemander(projectQuery);
    return CommonUtils.buildTableData(projects);
  }

  /**
   * 劳务方项目列表
   */
  @PostMapping("/listForLabor")
  public TableDataInfo<ProjectSimple> listForLabor(
      @Validated @RequestBody ProjectQueryForLabor projectQuery) {
    CommonPageInfo<ProjectSimple> projects = projectBusiness.listForLabor(projectQuery);
    return CommonUtils.buildTableData(projects);
  }

  /**
   * 平台方项目列表
   */
  @OperationAuth({UserTypeEnum.PLATFORM_USER})
  @PostMapping("/listForPlatform")
  public TableDataInfo<ProjectSimple> listForPlatform(
      @Validated @RequestBody ProjectQueryForPlatform projectQuery) {
    CommonPageInfo<ProjectSimple> projects = projectBusiness.listForPlatform(projectQuery);
    return CommonUtils.buildTableData(projects);
  }

  /**
   * 项目明细
   */
  @GetMapping("detail")
  public APIResult<ProjectDetail> detail(@RequestParam("projectId") String projectId) {
    final ProjectDetail projectDetail = projectBusiness.detail(projectId);
    return APIResult.success(projectDetail);
  }

  /**
   * 新建项目
   */
  @OperationAuth({UserTypeEnum.DEMANDER_USER})
  @ReSubmitFilter()
  @PostMapping("create")
  public APIResult<Void> create(@Validated @RequestBody ProjectObj projectObj) {
    projectBusiness.create(projectObj);
    return APIResult.success();
  }

  /**
   * 项目修改
   */
  @OperationAuth({UserTypeEnum.DEMANDER_USER, UserTypeEnum.LABOR_USER})
  @PostMapping("modify")
  public APIResult<Void> modify(@Validated @RequestBody ProjectObj projectObj) {
    projectBusiness.modify(projectObj);
    return APIResult.success();
  }

  /**
   * 项目变更记录
   */
  @PostMapping("changeRecord")
  public TableDataInfo<ProjectChangeRecord> changeRecord(
      @Validated @RequestBody ProjectLogBaseQuery query) {
    CommonPageInfo<ProjectChangeRecord> changeRecords = projectBusiness.changeRecord(query);
    return CommonUtils.buildTableData(changeRecords);
  }

  /**
   * 项目发布
   */
  @OperationAuth({UserTypeEnum.DEMANDER_USER})
  @PostMapping("publish")
  public APIResult<Void> publish(@RequestParam("projectId") String projectId) {
    projectBusiness.publish(projectId);
    return APIResult.success();
  }

  /**
   * 项目打卡
   */
  @OperationAuth({UserTypeEnum.LABOR_USER})
  @PostMapping("finishWork")
  public APIResult<Void> finishWork(@RequestParam("projectId") String projectId) {
    projectBusiness.finishWork(projectId);
    return APIResult.success();
  }

  /**
   * 项目交付
   */
  @OperationAuth({UserTypeEnum.LABOR_USER})
  @PostMapping("delivery")
  public APIResult<Void> delivery(@Validated @RequestBody ProjectDelivery deliveryInfo) {
    projectBusiness.delivery(deliveryInfo);
    return APIResult.success();
  }

  /**
   * 项目验收
   */
  @OperationAuth({UserTypeEnum.DEMANDER_USER})
  @PostMapping("verify")
  public APIResult<Void> verify(@Validated @RequestBody ProjectVerify verify) {
    projectDeliveryBusiness.verify(verify);
    return APIResult.success();
  }

  /**
   * 上传备用金预算明细列表
   */
  @PostMapping("uploadReserveFundDetailList")
  public APIResult<UploadResult> uploadReserveFundDetailList(
      @RequestPart(value = "file") MultipartFile file) {
    UploadResult uploadResult = projectBusiness.uploadReserveFundDetailList(file);
    return APIResult.success(uploadResult);
  }

  /**
   * 上传需求列表
   */
  @PostMapping("uploadDemandList")
  public APIResult<UploadResult> uploadDemandList(@RequestPart(value = "file") MultipartFile file) {
    UploadResult uploadResult = projectBusiness.uploadDemandList(file);
    return APIResult.success(uploadResult);
  }

  /**
   * 上传团队列表
   */
  @PostMapping("uploadTeamList")
  public APIResult<TeamUploadResult> uploadTeamList(
      @RequestPart(value = "file") MultipartFile file) {
    TeamUploadResult teamUploadResult = projectBusiness.uploadTeamList(file);
    return APIResult.success(teamUploadResult);
  }

  /**
   * 上传备用金使用明细列表
   */
  @PostMapping("uploadReserveFundUsedDetailList")
  public APIResult<UploadResult> uploadReserveFundUsedDetailList(
      @RequestPart(value = "file") MultipartFile file) {
    UploadResult uploadResult = projectBusiness.uploadReserveFundDetailList(file);
    return APIResult.success(uploadResult);
  }

  /**
   * 上传项目报告
   */
  @PostMapping("uploadReport")
  public APIResult<UploadResult> uploadReport(@RequestPart(value = "file") MultipartFile file) {
    UploadResult uploadResult = projectBusiness.uploadReport(file);
    return APIResult.success(uploadResult);
  }

}
